package nu.mine.tbje.easyEDT;



import java.util.ArrayList;

import nu.mine.tbje.easyEDT.data.Cours;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;

/***
 *
 * @author Tbje
 *
 */
public class HTMLCallbackHandler extends HTMLEditorKit.ParserCallback {
	int row = 0;
	boolean read;
	boolean header = true;
	
	public ArrayList<Cours> listeCours;
	private Cours x;
	
	public HTMLCallbackHandler(){
		listeCours = new ArrayList<Cours>();
	}

	public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
		if (t == HTML.Tag.TR) {
			if (!header) {
				x = new Cours();
			}

		}
		if (t == HTML.Tag.TD) {
			read = true;
		}
	}

	public void handleEndTag(HTML.Tag t, int pos) {
		if (t == HTML.Tag.TR) {
			if (!header) {
				if (x!=null) {
					listeCours.add(x);
				}
				x = null;

			}
			header = false;

			row = 0;
		}
		if (t == HTML.Tag.TD) {
			read = false;
			row++;
		}
	}
	
    public void handleText(char[] data, int pos) {
    	try {
			String s = new String(data);
			s.replaceAll("&#160;", " ");
			if (read) {
				switch (row) {
				case 0:
					x.setDate(s);
					break;
				case 1:
					x.setTime(s);
					break;
				case 2:
					x.setDuration(s);
					break;
				case 3:
					x.setName(s);
					break;
				case 4:
					break;
				case 5:
					x.setTeacher(s);
					break;
				case 6:
					x.setRoom(s);
					break;
				default:
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
    public void handleEndOfLineString(String eol) {
    }


	
}
